﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MovieCollectionManager.DataModel;

namespace MovieCollectionManager
{
    public partial class ReturnLoanForm : Form
    {
        public ReturnLoanForm()
        {
            InitializeComponent();
        }

        private void ReturnLoanForm_Load(object sender, EventArgs e)
        {
            listBoxMoviesLent.DisplayMember = "Title";
            listBoxMoviesLent.ValueMember = "Id";
            BindLoans();
        }

        private void BindLoans()
        {
            listBoxMoviesLent.Items.Clear();

            using (var db = new MovieCollectionConteiner())
            {
                var loansResult = (from loan in db.Loans
                                   select loan.Receiver).Distinct();

                listBoxLoanList.DataSource = loansResult.ToList();
            }
        }

        private void listBoxLoanList_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBoxMoviesLent.Items.Clear();

            using (var db = new MovieCollectionConteiner())
            {
                var loanMoviesResult = (from loan in db.Loans
                                        where loan.Receiver == listBoxLoanList.Text
                                        select loan.MovieId);

                foreach (int movieId in loanMoviesResult.ToList())
                {
                    Movie moviesResult = (from movie in db.Movies
                                          where movie.Id == movieId
                                          select movie).First();

                    listBoxMoviesLent.Items.Add(moviesResult);
                }
            }
        }

        private void buttonReturnLoan_Click(object sender, EventArgs e)
        {
            if ((listBoxLoanList.Items.Count == 0) || (listBoxLoanList.SelectedItem == null))
                return;

            if (MessageBox.Show("Deseja realmente retornar o empréstimo selecionado?", "Confirmar retorno", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            using (var db = new MovieCollectionConteiner())
            {
                var loanToReturn = (from loan in db.Loans
                                    where loan.Receiver == listBoxLoanList.Text
                                    select loan);
                foreach (Loan loan in loanToReturn.ToList())
                    db.Loans.DeleteObject(loan);

                db.SaveChanges();
            }

            UpdateMovieLentStatus();
            BindLoans();
            MessageBox.Show("Empréstimo retornado!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void UpdateMovieLentStatus()
        {
            using (var db = new MovieCollectionConteiner())
            {
                foreach (Movie movie in listBoxMoviesLent.Items)
                {
                    Movie movieToUpdate = (from movie1 in db.Movies
                                           where movie1.Id == movie.Id
                                           select movie1).First();
                    movieToUpdate.Lent = false;
                }
                db.SaveChanges();
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
